//
//  HMHomeRetweetView.swift
//  WB25
//
//  Created by HM on 2017/7/4.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit
import SnapKit
import YYText
class HMHomeRetweetView: UIView {
    
    // 记录转发微博的底部约束
    var retweetViewBottomConstraint: Constraint?
    
    // 提供属性供外界赋值
    var statusViewModel: HMStatusViewModel?{
        didSet{
            // 赋值
            contentLabel.attributedText = statusViewModel?.retweetAttributedText
            
            /*
                - 卸载约束
                - 判断是否有配图
                    - 判断条件: statusViewModel?.homeModel?.retweeted_status?.pic_urls
                    - 如果pic_urls.count > 0
                        - 有配图
                            - 赋值
                            - retweetView.bottom = pictureView.bottom + 10
                            - 显示配图
                        - 反之没有配图
                            - retweetView.bottom = contentLabel.bottom + 10
                            - 隐藏配图

             */
            
            // 卸载约束
            retweetViewBottomConstraint?.deactivate()
            
            // 有配图
            if let pic_urls = statusViewModel?.homeModel?.retweeted_status?.pic_urls, pic_urls.count > 0 {
                // 赋值
                pictureView.picUrlsList = pic_urls
                snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                pictureView.isHidden = false
            }else{
                // 没有配图
                snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                
                pictureView.isHidden = true
            }
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        // 添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        // 添加约束
        contentLabel.snp.makeConstraints { (make) in
            make.top.left.equalTo(10)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
        }
        
        // 转发微博的底部约束
        snp.makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        
        // 监听高亮文字点击 后触发事件
        contentLabel.highlightTapAction = { (containerView, text, range, rect) -> () in
            print(containerView, text, range, rect)
            // push一个新控制器
        }
    
    }

    //MARK: - 懒加载控件
    // 微博正文
    private lazy var contentLabel: YYLabel = {
        // 由于中文、英文、Emoji 等字体高度不一致，或者富文本中出现了不同字号的字体，
        // 可能会造成每行文字的高度不一致。这里可以添加一个修改器来实现固定行高，或者自定义文本行位置。
        let modifier = YYTextLinePositionSimpleModifier()
        modifier.fixedLineHeight = 24
        let lab = YYLabel()
        lab.linePositionModifier = modifier
        lab.textColor = UIColor.black
        //        lab.font = UIFont.systemFont(ofSize: HMNormalFontSize)
        lab.preferredMaxLayoutWidth = HMSCREENW - 20
        lab.numberOfLines = 0
        return lab
    }()
    // 配图
    private lazy var pictureView: HMHomePictureView = {
        let view = HMHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()}
